const app = getApp()

Page({
  data: {
    // 经度  维度
    longitudeMe: 120.2,
    latitudeMe: 30.3,
    windowHeight: "200px",
    windowWidth: "200px",
    controls: [{
      id: 1,
      iconPath: '../../images/loveMap/dingwei.png',
      position: {
        left: 20,
        top: 500-160,
        width: 40,
        height: 40
      },
      clickable: true
    }],
    markers:[]
  },
  onReady: function (e) {
    var _this = this;
    _this.mapCtx = wx.createMapContext('map')
  },
  getNearActivity:function(){
    var _this = this;
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy
    app.doReq(app.globalData.SERVICE.INDEX.MEMBERDT, {
      longitude: longitude,
      latitude: latitude,
      id: app.globalData.userid
    }, 'post', function (res) {
      console.log(res);
      var data = res.data;
      if (data.code == 200) {
        var list = data.obj.child;
        var marker = [];
        for (var i in list) {
          var item = {};
          let label = {};
          if (list[i].sex == '男') {
            item.iconPath = "../../images/loveMap/mapicon_man.png";
            label.color = '#008aff';
            label.borderColor = '#008aff';
          } else if (list[i].sex == '女') {
            item.iconPath = "../../images/loveMap/mapicon_woman.png";
            label.color = '#e60012';
            label.borderColor = '#e60012';
          }
          item.id = list[i].id;
          item.latitude = list[i].latitude;
          item.longitude = list[i].longitude;
          item.width = 30;
          item.height = 30;

          label.content = list[i].nickName
          label.borderWidth = 1,
          label.borderRadius = 4;
          label.bgColor = '#ffffff';
          label.padding = 4;
          label.display = 'BYCLICK';

          item.label = label;

          let callout={};
          callout.content="测试";
          callout.display ="BYCLICK";
          item.callout=callout;
          
          marker.push(item);
        }
        _this.setData({
          markers: marker
        })
      }
    });
  },
  getNearPeople:function(res){
        var _this = this;
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        app.doReq(app.globalData.SERVICE.INDEX.MEMBERDT, {
          longitude: longitude,
          latitude: latitude,
          id: app.globalData.userid
        }, 'post', function (res) {
          console.log(res);
          var data = res.data;
          if (data.code == 200) {
            var list = data.obj.child;
            var marker = [];
            for (var i in list) {
              var item = {};
              let label = {};
              if (list[i].sex == '男') {
                item.iconPath = "../../images/loveMap/mapicon_man.png";
                label.color = '#008aff';
                label.borderColor = '#008aff';
              } else if (list[i].sex == '女') {
                item.iconPath = "../../images/loveMap/mapicon_woman.png";
                label.color = '#e60012';
                label.borderColor = '#e60012';
              }
              item.id = list[i].id;
              item.latitude = list[i].latitude;
              item.longitude = list[i].longitude;
              item.width = 30;
              item.height = 30;

              label.content = list[i].nickName
              label.borderWidth = 1,
              label.borderRadius = 4;
              label.bgColor = '#ffffff';
              label.padding = 4;

              item.label = label;

              // let callout = {
              //   content: "老人头",
              //   bgColor: "#fff",
              //   color: "#f00",
              //   padding: 10,
              //   display: "",
              //   borderRadius: 5
              // };
              // callout.content = "测试";
              // callout.display = "BYCLICK";
              // item.callout = callout;

              marker.push(item);
            }
            _this.setData({
              markers: marker
            })
          }
        });
  },
  onLoad: function () {
    var _this=this;
    // wx.showLoading({
    //   title: '加载中....',
    // })
    var _this=this;
    wx.getSystemInfo({
      // 获取手机信息
      success: function (res) {
        var windowHeights = res.windowHeight;
        var windowWidths = res.windowWidth;
        var controlsTop = "controls[0].position.top";
        var controlsLeft = "controls[0].position.left";
        _this.setData({
          windowHeight: windowHeights + 'px',
          windowWidth: windowWidths + 'px',
          [controlsTop]: windowHeights - 100,
          [controlsLeft]: windowWidths - 56
        })
      }
    });

    wx.getLocation({
      success: function(res) {
        _this.setData({
          longitudeMe:res.longitude,
          latitudeMe: res.latitude
        });
        _this.getNearPeople(res);
      },
    })
  
  },
  markertap(e) {
    wx.navigateTo({
      url: '../../pages/memberDetails/memberDetails?userid='+e.markerId,
    })
  },
  controltap(e) {
    // 点击右下角图标  定位到定位点
    console.log('controlId', e.controlId)
    var _this = this
    _this.mapCtx.moveToLocation()
  }
})